<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="150px">
        <el-form-item label="用户id" prop="id">
          <el-input v-model="formData.uid" placeholder="请输入用户uid"/>
        </el-form-item>
        <el-form-item label="身份证正面国徽" prop="idCardZ">
          <ImageUpload v-model="formData.idCardZ" :limit="1"/>
        </el-form-item>
        <el-form-item label="身份证反面人脸" prop="idCardF">
          <ImageUpload v-model="formData.idCardF" :limit="1"/>
        </el-form-item>
        <el-form-item label="回购协议" prop="protocolUrl">
          <ImageUpload v-model="formData.protocolUrl" :limit="1"/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="身份证号码" prop="idCode">
          <el-input v-model="formData.idCode" placeholder="请输入身份证号码"/>
        </el-form-item>
        <el-form-item label="身份有效期" prop="idYxq">
          <el-input v-model="formData.idYxq" placeholder="请输入身份有效期"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="formData.status" placeholder="选择状态">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.APPROVAL_TYPE)"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="审核备注" prop="bz">
          <el-input v-model="formData.bz" placeholder="请输入审核备注"/>
        </el-form-item>
        <el-form-item label="认证时间" prop="authTime">
          <el-date-picker clearable v-model="formData.authTime" type="date" value-format="timestamp"
                          placeholder="选择认证时间"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as UserAuthApi from '@/api/system/userauth';
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "UserAuthForm",
  components: {
    ImageUpload
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        uid: undefined,
        idCardZ: undefined,
        idCardF: undefined,
        protocolUrl: undefined,
        name: undefined,
        idCode: undefined,
        idYxq: undefined,
        status: undefined,
        bz: undefined,
        authTime: undefined,
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await UserAuthApi.getUserAuth(id);
          this.formData = res.data;
          this.title = "修改用户认证";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增用户认证";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await UserAuthApi.updateUserAuth(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await UserAuthApi.createUserAuth(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        uid: undefined,
        idCardZ: undefined,
        idCardF: undefined,
        name: undefined,
        idCode: undefined,
        idYxq: undefined,
        status: undefined,
        bz: undefined,
        authTime: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
